package models

import (
	_ "time"

	orm "go-papers/database"
	"go-papers/tools"
)

type OrderPay struct {
	OrderPayId int     `json:"orderPayId" gorm:"type:int(11);primary_key"` //
	OrderId    int     `json:"orderId" gorm:"type:int(11);"`               //
	PayType    string  `json:"payType" gorm:"type:varchar(255);"`          // 支付类型  wx  ali local
	PayPrice   float64 `json:"payPrice" gorm:"type:decimal(10,2);"`        //
	PayDesc    string  `json:"payDesc" gorm:"type:varchar(255);"`          //
	PayCome    int     `json:"payCome" gorm:"type:tinyint(1);"`            // 1 付款， 2 退款
	PayNo      string  `json:"payNo" gorm:"type:varchar(128);"`            // 支付单号
	PayTime    string  `json:"payTime" gorm:"type:varchar(64);"`           //
	DataScope  string  `json:"dataScope" gorm:"-"`
	Params     string  `json:"params"  gorm:"-"`
	BaseModel
}

func (OrderPay) TableName() string {
	return "order_pay"
}

// 创建OrderPay
func (e *OrderPay) Create() (OrderPay, error) {
	var doc OrderPay
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

func (e *OrderPay) Get() (OrderPay, error) {
	var doc OrderPay
	table := orm.Eloquent.Table(e.TableName())

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取OrderPay
func (e *OrderPay) GetList() []OrderPay {
	var doc []OrderPay
	table := orm.Eloquent.Select("*").Table(e.TableName())

	if e.OrderId <= 0 {
		e.OrderId = -1
	}
	table = table.Where("order_id = ?", e.OrderId)
	if err := table.Find(&doc).Error; err != nil {
		return nil
	}
	return doc
}

// 获取OrderPay带分页
func (e *OrderPay) GetPage(pageSize int, pageIndex int) ([]OrderPay, int, error) {
	var doc []OrderPay

	table := orm.Eloquent.Select("*").Table(e.TableName())

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新OrderPay
func (e *OrderPay) Update(id int) (update OrderPay, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_pay_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除OrderPay
func (e *OrderPay) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_pay_id = ?", id).Delete(&OrderPay{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *OrderPay) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_pay_id in (?)", id).Delete(&OrderPay{}).Error; err != nil {
		return
	}
	Result = true
	return
}
